package com.vma.interfaceShoes.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.offer.service.MarketService;
import com.vma.offer.service.OfferService;
import com.vma.shoes.bizModel.model.Brand;
import com.vma.shoes.bizModel.model.OfferType;
import com.vma.shoes.bizModel.model.ShoesConfig;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenzui on 2017/4/20.
 */
public class OfferController extends Controller {
    public void getOfferDetail(){
        try {
            renderJson(ReturnsUtil.returnSuccess(OfferService.getOfferDetail(getPara("offerId"))));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void getOfferDetailForApp(){
        try {
            int userId = getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(OfferService.getOfferDetailForApp(getParaToInt("offerId"),userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void addKeep(){
        try {
            int userId =  getParaToInt("userId");
            int offerId =  getParaToInt("offerId");
            OfferService.addKeep(userId,offerId);
            renderJson(ReturnsUtil.returnSuccess("收藏成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void removeKeep(){
        try {
            int userId =  getParaToInt("userId");
            int offerId =  getParaToInt("offerId");
            OfferService.removeKeep(userId,offerId);
            renderJson(ReturnsUtil.returnSuccess("取消收藏成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryKeepOffersList(){
        try {
            int userId =  getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(OfferService.qryKeepOffersList(userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryOfferListNotInMarket(){
        try {
            int cPage =  getParaToInt("cPage");
            int pSize =  getParaToInt("pSize");
            int userId =  getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(OfferService.qryOfferListNotInMarket(cPage,pSize,userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * 根据关键字查询商品
     */
    public void qryOfferListByKeyWords(){
        try {
            int userId = getParaToInt("userId");
            String keyWord = getPara("keyWord");
            int cPage =  getParaToInt("cPage");
            int pSize =  getParaToInt("pSize");
            String action = getPara("action");
            renderJson(ReturnsUtil.returnSuccess(OfferService.qryOfferListByKeyWords(cPage,pSize,keyWord,userId,action)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void initSerchCond(){
        try {
            List<Brand> brands = OfferService.qryBrandList();
            List<OfferType> types = OfferService.qryTypeList();
            Map map = new HashMap();
            map.put("brands",brands);
            map.put("types",types);
            ShoesConfig config = ShoesConfig.dao.findById(1);
            map.put("price",config.getPriceOne()+","+config.getPriceTwo()+","+config.getPriceThree());
            renderJson(ReturnsUtil.returnSuccess(map));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
